﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.User;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Entity;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.Person.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class WageAttachmentRepository : Repository<WageAttachment, DefaultDbContext>
    {
        public WageAttachmentRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
        {
        }

        /// <summary>
        /// 根据objectId获取系统附件关联对象
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public IEnumerable<WageAttachment> GetAttachmentsByObjectId(object objectId)
        {
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("wage_attachment");
            sql.Where("object_id = @0  and is_deleted!=1", objectId);
            var ids = DbContext.FetchFirstColumn<string>(sql);
            return PopulateEntitiesByEntityIds(ids);
        }

        public IEnumerable<WageAttachment> GetAttachmentsByObjectIds(IEnumerable<object> objectId)
        {
            if (objectId == null || objectId.Count() == 0)
                return Enumerable.Empty<WageAttachment>();
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("wage_attachment");
            sql.Where("object_id in(@0)  and is_deleted!=1", objectId);
            var ids = DbContext.FetchFirstColumn<string>(sql);
            return PopulateEntitiesByEntityIds(ids);
        }
    }
}